package com.logic.landseaserver.models.printlog.service;

import com.logic.common.security.SecurityUtils;
import com.logic.landseaserver.common.exception.LandseaException;
import com.logic.landseaserver.common.exception.ResultCodes;
import com.logic.landseaserver.domain.PrintLog;
import com.logic.landseaserver.models.printlog.bean.PrintLogReq;
import com.logic.landseaserver.persistence.read.PrintLogReadMapper;
import com.logic.landseaserver.persistence.write.PrintLogWriteMapper;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * Created by Junny on 2018/7/17.
 * 打印记录业务处理类
 */
@Service
public class PrintLogService {

    @Autowired
    private PrintLogReadMapper printLogReadMapper;

    @Autowired
    private PrintLogWriteMapper printLogWriteMapper;

    public void savePrintLog(PrintLogReq req)throws LandseaException {
        if(req.getObjId()==null){
            throw LandseaException.createException(ResultCodes.PrintLogCode.E004700001);
        }
        if(StringUtils.isEmpty(req.getOprType())){
            throw LandseaException.createException(ResultCodes.PrintLogCode.E004700002);
        }
        Integer userId = SecurityUtils.getCurrentLogin().getUserId();
        PrintLog pLog = new PrintLog();
        pLog.setObjId(req.getObjId());
        pLog.setOprType(req.getOprType());
        pLog.setUserId(userId);
        pLog.setPrintTime(new Date());
        pLog.updateCommonInfo(userId);
        printLogWriteMapper.insertSelective(pLog);
    }

    public Integer queryCount(PrintLogReq req)throws LandseaException {
        if(req.getObjId()==null){
            throw LandseaException.createException(ResultCodes.PrintLogCode.E004700001);
        }
        if(StringUtils.isEmpty(req.getOprType())){
            throw LandseaException.createException(ResultCodes.PrintLogCode.E004700002);
        }
        Integer userId = SecurityUtils.getCurrentLogin().getUserId();
        PrintLog pLog = new PrintLog();
        pLog.setObjId(req.getObjId());
        pLog.setOprType(req.getOprType());
        pLog.setUserId(userId);
        List<PrintLog> printLogs =  printLogReadMapper.queryPrintLogs(pLog);
        return printLogs.size();
    }



}
